package com.tpshion.chat.controller;

import com.tpshion.chat.domain.dto.ReqFriendDto;
import com.tpshion.chat.domain.entity.ReqFriend;
import com.tpshion.chat.domain.model.ChatModel;
import com.tpshion.chat.domain.model.MessageModel;
import com.tpshion.chat.domain.model.MessageType;
import com.tpshion.chat.domain.vo.FriendsVo;
import com.tpshion.chat.domain.vo.ReqFriendVo;
import com.tpshion.chat.domain.vo.UserInfoVo;
import com.tpshion.chat.exception.ResponseVo;
import com.tpshion.chat.service.FriendService;
import com.tpshion.chat.service.ReqFriendService;
import com.tpshion.chat.service.UserInfoService;
import com.tpshion.chat.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * @Description: 好友管理
 * @Author: tpshion
 * @Date: 2020/5/3 12:54
 * @Version: 1.0
 */
@RestController
@Slf4j
public class FriendControler {

    @Autowired
    private FriendService friendService;

    @Autowired
    private ReqFriendService reqFriendService;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户id获取好友列表
     * @param userId
     * @return
     */
    @GetMapping(value = "/friends/{userId}")
    public ResponseVo<List<FriendsVo>> getListByUserId(@PathVariable("userId") String userId){
        List<FriendsVo> friends = friendService.getListByUserId(userId);
        return ResponseVo.ok(friends);
    }

    /**
     * 添加好友
     * @param reqFriendDto
     * @return
     */
    @PostMapping(value = "addFriend")
    public ResponseVo addFriend(@RequestBody ReqFriendDto reqFriendDto){
        ReqFriend reqFriend = new ReqFriend();
        reqFriend.setUserId(reqFriendDto.getUserId());
        reqFriend.setFriendId(reqFriendDto.getFriendId());
        reqFriend.setStatus(1);
        reqFriend.setTimestamp(new Date());
        reqFriendService.addFriend(reqFriend);
        UserInfoVo userInfoVo = userInfoService.getUsreByUserId(reqFriendDto.getUserId());
        ReqFriendVo reqFriendVo = new ReqFriendVo();
        BeanUtils.copyProperties(userInfoVo,reqFriendVo);
        reqFriendVo.setStatus(1);
        MessageModel messageModel = new MessageModel();
        messageModel.setType(MessageType.REQUEST.getType());
        messageModel.setData(reqFriendVo);
        webSocketServer.sendToUser(messageModel,reqFriendDto.getFriendId());
        return ResponseVo.ok("添加好友请求发送成功");
    }

    /**
     * 根据用户id获取好友请求列表
     * @param userId
     * @return
     */
    @GetMapping(value = "/getReqFriends/{userId}")
    public ResponseVo<List<ReqFriendVo>> getReqFriends(@PathVariable("userId") String userId){
        List<ReqFriendVo> reqFriendVos = reqFriendService.getListByUserId(userId);
        return ResponseVo.ok(reqFriendVos);
    }

    /**
     * 同意好友请求
     * @param reqFriendDto
     * @return
     */
    @PostMapping(value = "/agreeRequest")
    public ResponseVo agreeRequest(@RequestBody ReqFriendDto reqFriendDto){
        reqFriendService.updateStatus(reqFriendDto);
        UserInfoVo userInfoVo = userInfoService.getUsreByUserId(reqFriendDto.getUserId());
        ReqFriendVo reqFriendVo = new ReqFriendVo();
        BeanUtils.copyProperties(userInfoVo,reqFriendVo);
        reqFriendVo.setStatus(2);
        MessageModel messageModel = new MessageModel();
        messageModel.setType(MessageType.REQUEST.getType());
        messageModel.setData(reqFriendVo);
        webSocketServer.sendToUser(messageModel,reqFriendDto.getFriendId());
        return ResponseVo.ok("添加好友成功!");
    }

    /**
     * 拒绝添加好友
     * @param reqFriendDto
     * @return
     */
    @PostMapping(value = "/refuseRequest")
    public ResponseVo refuseRequest(@RequestBody ReqFriendDto reqFriendDto){
        reqFriendService.updateStatus(reqFriendDto);
        UserInfoVo userInfoVo = userInfoService.getUsreByUserId(reqFriendDto.getUserId());
        ReqFriendVo reqFriendVo = new ReqFriendVo();
        BeanUtils.copyProperties(userInfoVo,reqFriendVo);
        reqFriendVo.setStatus(3);
        MessageModel messageModel = new MessageModel();
        messageModel.setType(MessageType.REQUEST.getType());
        messageModel.setData(reqFriendVo);
        webSocketServer.sendToUser(messageModel,reqFriendDto.getFriendId());
        return ResponseVo.ok("拒绝添加好友成功!");
    }

    /**
     * 发送消息给用户
     * @param chatModel
     * @return
     */
    @PostMapping(value = "/sendToUser")
    public ResponseVo sendToUser(@RequestBody ChatModel chatModel){
        MessageModel<ChatModel> messageModel = new MessageModel<>();
        messageModel.setType(MessageType.CHAT.getType());
        messageModel.setData(chatModel);
        webSocketServer.sendToUser(messageModel,chatModel.getToUserId());
        return ResponseVo.ok("ok");
    }
}
